package design_patterns_and_best_practices_in_java;

interface ArithmeticComposite {
    public int getValue();
}

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/11/14 21:35
 */
public class Chapter4ComponentMode {
    public static void main(String[] args) {
        ArithmeticComposite expr = new MiMinusOperand(new PlusOperand(new NumericValue(1), new NumericValue(4)),
                new NumericValue(2));
        System.out.println("Value equals " + expr.getValue());
    }
}

abstract class ArithmeticOperand implements ArithmeticComposite {
    ArithmeticComposite left;
    ArithmeticComposite right;

    public ArithmeticOperand(ArithmeticComposite left, ArithmeticComposite right) {
        this.left = left;
        this.right = right;
    }
}

class NumericValue implements ArithmeticComposite {

    private int value;

    public NumericValue(int value) {
        this.value = value;
    }

    @Override
    public int getValue() {
        return value;
    }
}

class PlusOperand extends ArithmeticOperand {

    public PlusOperand(ArithmeticComposite left, ArithmeticComposite right) {
        super(left, right);
    }

    @Override
    public int getValue() {
        return left.getValue() + right.getValue();
    }
}

class MiMinusOperand extends ArithmeticOperand {

    public MiMinusOperand(ArithmeticComposite left, ArithmeticComposite right) {
        super(left, right);
    }

    @Override
    public int getValue() {
        return left.getValue() - right.getValue();
    }
}